﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.DAL;
using GameSchool.Models;
using GameSchool.Models.AccountModels;
using GameSchool.ViewModels.Home;
using System.Web.Security;

namespace GameSchool.Controllers
{
    public class HomeController : Controller
    {
        private IHomeRepository m_rep;

        public HomeController()
        {
            m_rep = new HomeRepository(new gsEntities());
        }

        public HomeController(IHomeRepository rep)
        {
            m_rep = rep;
        }

        // GET: /Home/Index
        [Authorize]
        public ActionResult Index()
        {
            return RedirectToAction("Recent");
        }

        // GET: /Home/Header
        [Authorize]
        public PartialViewResult Header()
        {
            var viewModel = new HeaderViewModel();
            viewModel.fullName = AccountProfile.CurrentUser.FullName;
            viewModel.departmentName = m_rep.GetDepartmentNameById(AccountProfile.CurrentUser.DepartmentDegreeId);

            return PartialView(viewModel);
        }

        // GET: /Home/Navigation
        [Authorize]
        public PartialViewResult Navigation()
        {
            var viewModel = new NavigationViewModel();
            var currentUser = Membership.GetUser();

            if (User.IsInRole("Admin"))
            {
                return PartialView("NavigationAdmin");
            }
            else if (User.IsInRole("Kennari"))
            {
                viewModel.courses = m_rep.GetCoursesTeacher((Guid)currentUser.ProviderUserKey);
                return PartialView("NavigationTeacher", viewModel);
            }
            else if (User.IsInRole("Nemandi"))
            {
                viewModel.courses = m_rep.GetCoursesStudent((Guid)currentUser.ProviderUserKey);
                return PartialView("NavigationStudent", viewModel);
            }

            throw new UnauthorizedAccessException("Þú virðist ekki vera í réttri notendagrúppu. Hafðu samband við kerfisstjóra.");
        }

        // GET: /Home/Recent
        [Authorize]
        public ActionResult Recent()
        {
            var viewModel = new RecentViewModel();

            if (User.IsInRole("Admin"))
            {
                viewModel.recentAdmin = m_rep.GetRecentAdmin();

                return View(viewModel);
            }
            else if (User.IsInRole("Kennari"))
            {
                viewModel.recentTeacher = m_rep.GetRecentTeacher();

                return View(viewModel);
            }
            else
            {
                viewModel.recentStudent = m_rep.GetRecentStudent();

                return View(viewModel);
            }
        }
    }
}
